package com.kzd.gtboon.main.access

import android.text.Editable
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import androidx.core.widget.addTextChangedListener
import com.kzd.gtboon.app.SPSConstance
import com.kzd.gtboon.custom.setOnClickAntiShakeListener
import com.kzd.gtboon.databinding.FragmentAccessSettingBinding
import com.kzd.gtboon.jetpack.vm.AccessViewModel
import com.kzd.gtboon.main.AccessAction
import com.module.lemlin.owner.OwnerViewBindingFragment
import com.module.lemlin.owner.ownerViewModel

class AccessSettingFragment : OwnerViewBindingFragment<FragmentAccessSettingBinding>() {

    companion object {
        fun newInstance(): AccessSettingFragment {
            return AccessSettingFragment()
        }
    }

    private val mViewModel: AccessViewModel by lazy {
        ownerViewModel(requireActivity())
    }

    override val inflate: (LayoutInflater, ViewGroup?, Boolean) -> FragmentAccessSettingBinding
        get() = FragmentAccessSettingBinding::inflate

    override fun initView() {

        fillData(mViewModel.mAccessAction)

        mViewBinding.etAccessSettingContent.addTextChangedListener {
            mViewBinding.tvAccessSettingSubmit.isEnabled = !it.isNullOrEmpty()
        }
        mViewBinding.tvAccessSettingSubmit.setOnClickAntiShakeListener {
            if (mViewModel.mAccessAction == AccessAction.SETTING_QQ) {
                mViewModel.userChange(qq = mViewBinding.etAccessSettingContent.text.toString())
            } else {
                mViewModel.userChange(pet_name = mViewBinding.etAccessSettingContent.text.toString())
            }
        }
    }

    override fun initViewMode() {

    }

    private fun fillData(action: AccessAction) {

        mViewBinding.tvAccessSettingLabel.text = if (action == AccessAction.SETTING_QQ)
            "Q\tQ" else "昵\t称"

        mViewBinding.etAccessSettingContent.inputType = if (action == AccessAction.SETTING_QQ)
            EditorInfo.TYPE_CLASS_NUMBER else EditorInfo.TYPE_CLASS_TEXT

        mViewBinding.etAccessSettingContent.hint = if (action == AccessAction.SETTING_QQ)
            "请填写QQ" else "请填写昵称"

        mViewBinding.etAccessSettingContent.text = if (action == AccessAction.SETTING_QQ) {
            Editable.Factory.getInstance().newEditable(SPSConstance.userInfo?.qq)
        } else {
            Editable.Factory.getInstance().newEditable(SPSConstance.userInfo?.pet_name)
        }
    }

}